package encryption.des;


import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

/**
 * des加密
 */
public class DesUtil {

    private final static Charset charset = StandardCharsets.UTF_8;


    /**
     * 加密
     *
     * @param srcStr
     * @param sKey
     * @return
     */
    public static String encrypt(String srcStr, String sKey) {
        byte[] src = srcStr.getBytes(charset);
        byte[] buf = Des.encrypt(src, sKey);
        return Des.parseByte2HexStr(buf);
    }

    /**
     * 解密
     *
     * @param srcStr
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String decrypt(String srcStr, String sKey) throws Exception {
        byte[] src = Des.parseHexStr2Byte(srcStr);
        byte[] buf = Des.decrypt(src, sKey);
        return new String(buf, charset);
    }

    // todo 测试编码
    public static void main(String[] args) throws Exception {

        // todo 字段加解密过程
        String demoStr = "123456789";
        String encryptResult = encrypt(demoStr, "eshore@1"); //2525113EB766DB238C9B2EF8A89CBEE0
        System.out.println(encryptResult);

        String decryptResult = decrypt("2525113EB766DB238C9B2EF8A89CBEE0", "eshore@1");
        System.out.println(decryptResult);

        // todo sign加解密过程
        String WEB_BACK_STR = "WEB_BACK_CALLBACK-" + System.currentTimeMillis();
        String sign = encrypt(WEB_BACK_STR, "eshore@1");
        System.out.println(sign);
        System.out.println(decrypt(sign, "eshore@1"));
    }
}
